package com.easemob.chat;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.media.AudioManager;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Build;
import android.os.Vibrator;
import com.easemob.EMEventListener;
import com.easemob.EMNotifierEvent;
import com.easemob.EMNotifierEvent.Event;
import com.easemob.util.EMLog;
import com.easemob.util.EasyUtils;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class EMNotifier
{
  private static final String TAG = "notify";
  static Ringtone ringtone = null;
  private static final String[] msg_eng = { "sent a message", "sent a picture", "sent a voice", "sent location message", "sent a video", "sent a file", "%1 contacts sent %2 messages" };
  private static final String[] msg_ch = { "发来一条消息", "发来一张图片", "发来一段语音", "发来位置信息", "发来一个视频", "发来一个文件", "%1个联系人发来%2条消息" };
  private static int notifyID = 341;
  private NotificationManager notificationManager = null;
  private HashSet<String> fromUsers = new HashSet();
  private int notificationNum = 0;
  private Context appContext;
  private String appName;
  private String packageName;
  private String[] msgs;
  private long lastNotifiyTime;
  private static EMNotifier instance;
  private OnMessageNotifyListener onMessageNotifyListener;
  private AudioManager audioManager;
  private Vibrator vibrator;
  private Hashtable<EMNotifierEvent.Event, List<EMEventListener>> filteredEventListeners = new Hashtable();
  private ExecutorService notifierThread = Executors.newSingleThreadExecutor();

  private EMNotifier(Context paramContext)
  {
    this.appContext = paramContext;
    if (this.notificationManager == null)
      this.notificationManager = ((NotificationManager)paramContext.getSystemService("notification"));
    this.onMessageNotifyListener = EMChatManager.getInstance().getChatOptions().onMessageNotifyListener;
    if (this.appContext.getApplicationInfo().labelRes != 0)
      this.appName = this.appContext.getString(this.appContext.getApplicationInfo().labelRes);
    else
      this.appName = "";
    this.packageName = this.appContext.getApplicationInfo().packageName;
    if (Locale.getDefault().getLanguage().equals("zh"))
      this.msgs = msg_ch;
    else
      this.msgs = msg_eng;
    this.audioManager = ((AudioManager)this.appContext.getSystemService("audio"));
    this.vibrator = ((Vibrator)this.appContext.getSystemService("vibrator"));
  }

  public void clear()
  {
    this.filteredEventListeners.clear();
  }

  public static synchronized EMNotifier getInstance(Context paramContext)
  {
    if (instance == null)
    {
      instance = new EMNotifier(paramContext);
      return instance;
    }
    return instance;
  }

  public void stop()
  {
    if (ringtone != null)
    {
      ringtone.stop();
      ringtone = null;
    }
  }

  public void registerEventListener(EMEventListener paramEMEventListener)
  {
    if (paramEMEventListener == null)
      return;
    registerEventListener(paramEMEventListener, new EMNotifierEvent.Event[] { EMNotifierEvent.Event.EventNewCMDMessage, EMNotifierEvent.Event.EventNewMessage, EMNotifierEvent.Event.EventDeliveryAck, EMNotifierEvent.Event.EventReadAck });
  }

  public void removeEventListener(EMEventListener paramEMEventListener)
  {
    if (paramEMEventListener == null)
      return;
    synchronized (this.filteredEventListeners)
    {
      Collection localCollection = this.filteredEventListeners.values();
      if (localCollection != null)
      {
        Iterator localIterator = localCollection.iterator();
        while (localIterator.hasNext())
          remove((List)localIterator.next(), paramEMEventListener);
      }
    }
  }

  public void registerEventListener(EMEventListener paramEMEventListener, EMNotifierEvent.Event[] paramArrayOfEvent)
  {
    synchronized (this.filteredEventListeners)
    {
      for (EMNotifierEvent.Event localEvent : paramArrayOfEvent)
        registerEventListener(paramEMEventListener, localEvent);
    }
  }

  private void remove(List<EMEventListener> paramList, EMEventListener paramEMEventListener)
  {
    Iterator localIterator = paramList.iterator();
    while (localIterator.hasNext())
    {
      EMEventListener localEMEventListener = (EMEventListener)localIterator.next();
      if (localEMEventListener == paramEMEventListener)
        localIterator.remove();
    }
  }

  boolean publishEvent(final EMNotifierEvent.Event paramEvent, final Object paramObject)
  {
    if (containsType(paramEvent))
    {
      this.notifierThread.submit(new Runnable()
      {
        public void run()
        {
          synchronized (EMNotifier.this.filteredEventListeners)
          {
            if (EMNotifier.this.containsType(paramEvent))
            {
              List localList = (List)EMNotifier.this.filteredEventListeners.get(paramEvent);
              if (localList != null)
              {
                Iterator localIterator = localList.iterator();
                EMNotifier.this.publishEvent(localIterator, paramEvent, paramObject);
              }
            }
          }
        }
      });
      return true;
    }
    return false;
  }

  private void registerEventListener(EMEventListener paramEMEventListener, EMNotifierEvent.Event paramEvent)
  {
    Object localObject;
    if (!this.filteredEventListeners.containsKey(paramEvent))
    {
      localObject = new ArrayList();
      ((List)localObject).add(paramEMEventListener);
      this.filteredEventListeners.put(paramEvent, localObject);
    }
    else
    {
      localObject = (List)this.filteredEventListeners.get(paramEvent);
      if (!((List)localObject).contains(paramEMEventListener))
        ((List)localObject).add(0, paramEMEventListener);
    }
  }

  private boolean containsType(EMNotifierEvent.Event paramEvent)
  {
    return this.filteredEventListeners.containsKey(paramEvent);
  }

  private void publishEvent(Iterator<EMEventListener> paramIterator, EMNotifierEvent.Event paramEvent, Object paramObject)
  {
    while (paramIterator.hasNext())
    {
      EMNotifierEvent localEMNotifierEvent = new EMNotifierEvent();
      localEMNotifierEvent.setEventData(paramObject);
      localEMNotifierEvent.setEvent(paramEvent);
      EMEventListener localEMEventListener = (EMEventListener)paramIterator.next();
      if (localEMEventListener != null)
        localEMEventListener.onEvent(localEMNotifierEvent);
    }
  }

  void resetNotificationCount()
  {
    this.notificationNum = 0;
    this.fromUsers.clear();
  }

  synchronized void notifyChatMsg(EMMessage paramEMMessage)
  {
    if (publishEvent(EMNotifierEvent.Event.EventNewMessage, paramEMMessage))
      return;
    if (paramEMMessage.getBooleanAttribute("em_ignore_notification", false))
    {
      if (EasyUtils.isAppRunningForeground(this.appContext))
        sendBroadcast(paramEMMessage);
      return;
    }
    String str = null;
    List localList = null;
    if (paramEMMessage.chatType == EMMessage.ChatType.Chat)
    {
      str = paramEMMessage.getFrom();
      localList = EMChatManager.getInstance().getChatOptions().getUsersOfNotificationDisabled();
    }
    else
    {
      str = paramEMMessage.getTo();
      localList = EMChatManager.getInstance().getChatOptions().getGroupsOfNotificationDisabled();
    }
    if ((EMChatManager.getInstance().getChatOptions().isShowNotificationInBackgroud()) && (!EasyUtils.isAppRunningForeground(this.appContext)))
    {
      EMLog.d("notify", "easemob chat app is not running, sending notification");
      if ((localList == null) || (!localList.contains(str)))
      {
        sendNotification(paramEMMessage);
        notifyOnNewMsg();
      }
      return;
    }
    sendBroadcast(paramEMMessage);
    if ((EMChat.getInstance().appInited) && ((localList == null) || (!localList.contains(str))))
      notifyOnNewMsg();
  }

  void sendReadAckMsgBroadcast(String paramString1, String paramString2)
  {
    if (publishEvent(EMNotifierEvent.Event.EventReadAck, EMChatManager.getInstance().getMessage(paramString2)))
      return;
    Intent localIntent = new Intent(EMChatManager.getInstance().getAckMessageBroadcastAction());
    localIntent.putExtra("msgid", paramString2);
    localIntent.putExtra("from", paramString1);
    EMLog.d("notify", "send ack message broadcast for msg:" + paramString2);
    this.appContext.sendOrderedBroadcast(localIntent, null);
  }

  void sendDeliveryAckMsgBroadcast(String paramString1, String paramString2)
  {
    if (publishEvent(EMNotifierEvent.Event.EventDeliveryAck, EMChatManager.getInstance().getMessage(paramString2)))
      return;
    Intent localIntent = new Intent(EMChatManager.getInstance().getDeliveryAckMessageBroadcastAction());
    localIntent.putExtra("msgid", paramString2);
    localIntent.putExtra("from", paramString1);
    EMLog.d("notify", "send delivery ack message broadcast for msg:" + paramString2);
    this.appContext.sendOrderedBroadcast(localIntent, null);
  }

  void sendBroadcast(EMMessage paramEMMessage)
  {
    Intent localIntent = new Intent(EMChatManager.getInstance().getNewMessageBroadcastAction());
    localIntent.putExtra("msgid", paramEMMessage.msgId);
    localIntent.putExtra("from", paramEMMessage.from.username);
    localIntent.putExtra("type", paramEMMessage.type.ordinal());
    EMLog.d("notify", "send new message broadcast for msg:" + paramEMMessage.msgId);
    this.appContext.sendOrderedBroadcast(localIntent, null);
  }

  private void sendNotification(EMMessage paramEMMessage)
  {
    String str1 = paramEMMessage.from.getNick();
    try
    {
      String str2 = "";
      Object localObject1 = str1 + " ";
      switch ($SWITCH_TABLE$com$easemob$chat$EMMessage$Type()[paramEMMessage.type.ordinal()])
      {
      case 1:
        localObject1 = localObject1 + this.msgs[0];
        localObject2 = (TextMessageBody)paramEMMessage.body;
        str2 = ((TextMessageBody)localObject2).message;
        break;
      case 2:
        localObject1 = localObject1 + this.msgs[1];
        break;
      case 5:
        localObject1 = localObject1 + this.msgs[2];
        break;
      case 4:
        localObject1 = localObject1 + this.msgs[3];
        break;
      case 3:
        localObject1 = localObject1 + this.msgs[4];
        break;
      case 6:
        localObject1 = localObject1 + this.msgs[5];
        localObject3 = (FileMessageBody)paramEMMessage.body;
        str2 = ((FileMessageBody)localObject3).fileName;
      }
      this.onMessageNotifyListener = EMChatManager.getInstance().getChatOptions().onMessageNotifyListener;
      Object localObject2 = this.appContext.getPackageManager();
      Object localObject3 = (String)((PackageManager)localObject2).getApplicationLabel(this.appContext.getApplicationInfo());
      Object localObject4 = localObject3;
      if (this.onMessageNotifyListener != null)
      {
        localObject5 = this.onMessageNotifyListener.onNewMessageNotify(paramEMMessage);
        localObject6 = this.onMessageNotifyListener.onSetNotificationTitle(paramEMMessage);
        if (localObject5 != null)
          localObject1 = localObject5;
        if (localObject6 != null)
          localObject4 = localObject6;
      }
      Object localObject5 = new NotificationCompat.Builder(this.appContext).setSmallIcon(this.appContext.getApplicationInfo().icon).setWhen(System.currentTimeMillis()).setAutoCancel(true);
      Object localObject6 = this.appContext.getPackageManager().getLaunchIntentForPackage(this.packageName);
      if (EMChatManager.getInstance().getChatOptions().onNotificationClickListener != null)
        localObject6 = EMChatManager.getInstance().getChatOptions().onNotificationClickListener.onNotificationClick(paramEMMessage);
      PendingIntent localPendingIntent = PendingIntent.getActivity(this.appContext, notifyID, (Intent)localObject6, 268435456);
      this.notificationNum += 1;
      this.fromUsers.add(paramEMMessage.getFrom());
      int i = this.fromUsers.size();
      Object localObject7 = this.msgs[6].replaceFirst("%1", Integer.toString(i)).replaceFirst("%2", Integer.toString(this.notificationNum));
      if (this.onMessageNotifyListener != null)
      {
        localObject8 = this.onMessageNotifyListener.onLatestMessageNotify(paramEMMessage, i, this.notificationNum);
        if (localObject8 != null)
          localObject7 = localObject8;
        int j = this.onMessageNotifyListener.onSetSmallIcon(paramEMMessage);
        if (j != 0)
          ((NotificationCompat.Builder)localObject5).setSmallIcon(j);
      }
      ((NotificationCompat.Builder)localObject5).setContentTitle(localObject4);
      ((NotificationCompat.Builder)localObject5).setTicker((CharSequence)localObject1);
      ((NotificationCompat.Builder)localObject5).setContentText((CharSequence)localObject7);
      ((NotificationCompat.Builder)localObject5).setContentIntent(localPendingIntent);
      Object localObject8 = ((NotificationCompat.Builder)localObject5).build();
      try
      {
        this.notificationManager.cancel(notifyID);
      }
      catch (Exception localException2)
      {
      }
      this.notificationManager.notify(notifyID, (Notification)localObject8);
    }
    catch (Exception localException1)
    {
      localException1.printStackTrace();
    }
  }

  public void notifyOnNewMsg()
  {
    if ((!EMChatManager.getInstance().getChatOptions().getNotificationEnable()) || (!EMChatManager.getInstance().getChatOptions().getNotifyBySoundAndVibrate()))
      return;
    try
    {
      if (System.currentTimeMillis() - this.lastNotifiyTime < 1000L)
        return;
      this.lastNotifiyTime = System.currentTimeMillis();
      if (this.audioManager.getRingerMode() == 0)
      {
        EMLog.e("notify", "in slient mode now");
        return;
      }
      Object localObject1;
      if (EMChatManager.getInstance().getChatOptions().getNoticedByVibrate())
      {
        localObject1 = new long[] { 0, 180L, 80L, 120L };
        this.vibrator.vibrate((long[])localObject1, -1);
      }
      if (EMChatManager.getInstance().getChatOptions().getNoticedBySound())
      {
        localObject1 = Build.MANUFACTURER;
        Object localObject2;
        if (ringtone == null)
        {
          localObject2 = null;
          if (EMChatManager.getInstance().getChatOptions().getNotifyRingUri() == null)
            localObject2 = RingtoneManager.getDefaultUri(2);
          else
            localObject2 = EMChatManager.getInstance().getChatOptions().getNotifyRingUri();
          ringtone = RingtoneManager.getRingtone(this.appContext, (Uri)localObject2);
          if (ringtone == null)
          {
            EMLog.d("notify", "cant find ringtone at:" + ((Uri)localObject2).getPath());
            return;
          }
        }
        if (!ringtone.isPlaying())
        {
          ringtone.play();
          if ((localObject1 != null) && (((String)localObject1).toLowerCase().contains("samsung")))
          {
            localObject2 = new Thread()
            {
              public void run()
              {
                try
                {
                  Thread.sleep(3000L);
                  if (EMNotifier.ringtone.isPlaying())
                    EMNotifier.ringtone.stop();
                }
                catch (Exception localException)
                {
                }
              }
            };
            ((Thread)localObject2).run();
          }
        }
      }
    }
    catch (Exception localException)
    {
      localException.printStackTrace();
    }
  }

  void cancelNotificaton()
  {
    if (this.notificationManager != null)
      this.notificationManager.cancel(notifyID);
  }

  void sendIncomingVoiceCallBroadcast(String paramString1, String paramString2)
  {
    String str = EMContactManager.getUserNameFromEid(paramString1);
    Intent localIntent1 = new Intent(EMChatManager.getInstance().getIncomingCallBroadcastAction());
    localIntent1.putExtra("from", str);
    localIntent1.putExtra("type", paramString2);
    EMLog.d("notify", "send incoming call broadcaset with user : " + str);
    if (paramString2 == "audio")
    {
      Intent localIntent2 = new Intent(EMChatManager.getInstance().getIncomingVoiceCallBroadcastAction());
      localIntent2.putExtra("from", str);
      this.appContext.sendBroadcast(localIntent2, null);
    }
    this.appContext.sendBroadcast(localIntent1, null);
  }
}

/* Location:           D:\hygieia\userclient\common\libs\easemobchat_2.1.8.jar
 * Qualified Name:     com.easemob.chat.EMNotifier
 * JD-Core Version:    0.6.1
 */